package br.com.educa.modelo.disciplinar;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.TypedQuery;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

import br.com.educa.modelo.academico.Alunos;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

@Entity
@Table
@Configurable
@RooJson
@RooSerializable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findElogiosesByAluno" })
public class Elogios implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @ManyToOne
    @JoinColumn(name = "elogioTipo", referencedColumnName = "id")
    private ElogiosTipos elogioTipo;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(style = "M-")
    private Date dataRegistro;

    private String referencia;

    private String descricao;

    @ManyToOne
    @JoinColumn(name = "aluno", referencedColumnName = "id")
    private Alunos aluno;

    private static final long serialVersionUID = 1L;

    @PersistenceContext
    transient EntityManager entityManager;

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Elogios().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countElogioses() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Elogios o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.disciplinar.Elogios> findAllElogioses() {
        return entityManager().createQuery("SELECT o FROM Elogios o", Elogios.class).getResultList();
    }

    public static br.com.educa.modelo.disciplinar.Elogios findElogios(Long id) {
        if (id == null) return null;
        return entityManager().find(Elogios.class, id);
    }

    public static List<br.com.educa.modelo.disciplinar.Elogios> findElogiosEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Elogios o", Elogios.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Elogios attached = Elogios.findElogios(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.disciplinar.Elogios merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Elogios merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.disciplinar.Elogios fromJsonToElogios(String json) {
        return new JSONDeserializer<Elogios>().use(null, Elogios.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.disciplinar.Elogios> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.disciplinar.Elogios> fromJsonArrayToElogioses(String json) {
        return new JSONDeserializer<List<Elogios>>().use(null, ArrayList.class).use("values", Elogios.class).deserialize(json);
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public ElogiosTipos getElogioTipo() {
        return this.elogioTipo;
    }

    public void setElogioTipo(ElogiosTipos elogioTipo) {
        this.elogioTipo = elogioTipo;
    }

    public Date getDataRegistro() {
        return this.dataRegistro;
    }

    public void setDataRegistro(Date dataRegistro) {
        this.dataRegistro = dataRegistro;
    }

    public String getReferencia() {
        return this.referencia;
    }

    public void setReferencia(String referencia) {
        this.referencia = referencia;
    }

    public String getDescricao() {
        return this.descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

	public static TypedQuery<Elogios> findElogiosesByAluno(Alunos aluno) throws Exception {
        if (aluno == null) throw new IllegalArgumentException("The aluno argument is required");
        EntityManager em = Elogios.entityManager();
        Calendar cal = GregorianCalendar.getInstance();  
        int ano = cal.get(Calendar.YEAR); 
        DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" ); 
        String dti = String.valueOf(ano)+"-01-01";
        String dtf = String.valueOf(ano)+"-12-31";
        Date dtInicio = sdf.parse( dti );  
        Date dtFim = sdf.parse( dtf );         
        //TypedQuery<Elogios> q = em.createQuery("SELECT o FROM Elogios AS o WHERE o.aluno = :aluno AND o.dataRegistro >= :dtInicio ORDER BY o.dataRegistro DESC", Elogios.class);
        TypedQuery<Elogios> q = em.createQuery("SELECT o FROM Elogios AS o WHERE o.aluno = :aluno ORDER BY o.dataRegistro DESC", Elogios.class);
        q.setParameter("aluno", aluno);
        //q.setParameter("dtInicio", dtInicio);
        return q;
    }

	public Alunos getAluno() {
        return this.aluno;
    }

	public void setAluno(Alunos aluno) {
        this.aluno = aluno;
    }
}
